package edu.boun.g4.coursity.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.boun.g4.coursity.domain.Language;
import edu.boun.g4.coursity.service.LanguageService;

/**
 * @author mertusta
 * 
 *         Handles and retrieves language request
 */
@Controller
@RequestMapping("/language")
public class LanguageController {

	@Resource(name = "languageService")
	private LanguageService languageService;

	@RequestMapping(method = RequestMethod.POST, value = "/add")
	public @ResponseBody
	Response addLanguage(@RequestParam String name) {

		Language language = new Language();
		language.setLanguageName(name);

		Response response = new Response();
		response.setBody(language);
		response.setStatus(true);

		languageService.add(language);

		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/delete")
	public @ResponseBody
	Response deleteLanguage(@RequestParam Integer id) {

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		languageService.delete(id);

		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/edit")
	public @ResponseBody
	Response editLanguage(@RequestParam String name) {

		Language existingLanguage = new Language();
		existingLanguage.setLanguageName(name);

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		languageService.edit(existingLanguage);

		return response;
	}

	@RequestMapping(value = "/list")
	public @ResponseBody
	Response listUser() {

		List<Language> all = languageService.getAll();

		Response response = new Response();
		response.setBody(all);
		response.setStatus(true);

		return response;
	}

}